package com.zeny.express.framework.webapp.support;

import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;

import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.ResourceBundleTextProvider;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 重载struts2国际化
 * 
 * @author zhuwei
 * @date 2013年8月24日 上午10:19:20
 */
public class I18nTextProvider implements ResourceBundleTextProvider {

	protected static final Logger LOGGER = LoggerFactory
			.getLogger(I18nTextProvider.class);

	protected LocaleProvider localeProvider;

	@Autowired
	protected MessageSource messageSource;

	@Override
	public void setBundle(ResourceBundle bundle) {
		// Nothing to do
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void setClazz(Class clazz) {
		// Nothing to do
	}

	@Override
	public void setLocaleProvider(LocaleProvider localeProvider) {
		this.localeProvider = localeProvider;
	}

	@Override
	public boolean hasKey(String key) {

		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, null, locale);
		return StringUtils.isNotEmpty(value);
	}

	@Override
	public String getText(String key) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, null, locale);
		return value;
	}

	@Override
	public String getText(String key, String defaultValue) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource
				.getMessage(key, null, defaultValue, locale);
		return value;
	}

	@Override
	public String getText(String key, String defaultValue, String obj) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, new Object[] { obj },
				defaultValue, locale);
		return value;
	}

	@Override
	public String getText(String key, List<?> args) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, args.toArray(), locale);
		return value;
	}

	@Override
	public String getText(String key, String[] args) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, args, locale);
		return value;
	}

	@Override
	public String getText(String key, String defaultValue, List<?> args) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, args.toArray(),
				defaultValue, locale);
		return value;
	}

	@Override
	public String getText(String key, String defaultValue, String[] args) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource
				.getMessage(key, args, defaultValue, locale);
		return value;
	}

	@Override
	public String getText(String key, String defaultValue, List<?> args,
			ValueStack stack) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource.getMessage(key, args.toArray(),
				defaultValue, locale);
		return value;
	}

	@Override
	public String getText(String key, String defaultValue, String[] args,
			ValueStack stack) {
		Locale locale = localeProvider.getLocale();
		String value = messageSource
				.getMessage(key, args, defaultValue, locale);
		return value;
	}

	@Override
	public ResourceBundle getTexts(String bundleName) {
		return null;
	}

	@Override
	public ResourceBundle getTexts() {
		return null;
	}

}
